package com.jp.code.leetCode;

/**
 * https://leetcode.cn/problems/fibonacci-number/
 * <p>
 * F(0) = 0，F(1) = 1
 * F(n) = F(n - 1) + F(n - 2)，其中 n > 1
 */
public class Fib {


    public static int fib(int n) {
        if (n <= 1) {
            return n;
        }
        return fib(n - 1) + fib(n - 2);
    }


    public static int fib2(int n) {
        // 滚动数组
        if (n < 2) {
            return n;
        }
        int p = 0, q = 0, r = 1;
        for (int i = 2; i <= n; ++i) {
            p = q;
            q = r;
            r = p + q;
        }
        return r;
    }

    public static void main(String[] args) {


        // 1 2 3 4 5 6 7  8
        // 1 1 2 3 5 8 13 21
        System.out.println(fib(3));
        System.out.println(fib2(3));
    }

}
